<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/4/1 0001
 * Time: 10:54
 */

namespace app\common\widget;
use app\common\library\CommonFun;

/**
 * Class WidgetTableContainer
 * @package app\common\widget
 * @method WidgetTableContainer setWidthColXs($value) 设置Xs(min-width:0px)宽度值,$value=1到12
 * @method WidgetTableContainer setWidthColSm($value) 设置Sm(min-width:768px)宽度值,$value=1到12
 * @method WidgetTableContainer setWidthColMd($value) 设置Md(min-width:992px)宽度值,$value=1到12
 * @method WidgetTableContainer setWidthColLg($value) 设置Lg(min-width:1200px)宽度值,$value=1到12
 *
 * @method WidgetDataListTable addDataListTable( $sort=0 ) 添加一个数据列表
 */
class WidgetTableContainer extends WidgetCommon
{

    /**
     * 宽度列表
     * @var array
     */
    private $widthList = [
        'xs'=>WidgetConst::colxs12,
        'sm'=>WidgetConst::colsm12,
        'md'=>WidgetConst::colmd12,
        'lg'=>WidgetConst::collg12,
    ];

    /**
     * 容器子元素列表
     * @var array
     */
    private $childList = [];

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     */
    function render()
    {
        $this->beforeRender();

        // assign childlist
        if ( CommonFun::array_is_assoc( $this->childList ) === false ) ksort( $this->childList );
        self::$view->assign('childList', $this->childList);

        return $this->fetch();
    }

    /**
     * Before Render
     * @return $this
     * @throws \think\Exception
     */
    private function beforeRender() {
        // 检测
        if ( !$this->childList ) $this->throwException("容器挂件没有子元素");
        // 宽度处理
        $this->setClass( $this->widthList );
        // 子元素处理
        foreach ( $this->childList as &$v ) $v = $v->render();

        return $this;
    }

    /**
     * 初始化函数
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置默认宽度
        foreach ( $this->widthList as $media=>$v ) {
            $this->setWidth( $media, $v );
        }

        $this->setClass("row");
    }

    /**
     * __call 魔术方法
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \think\Exception
     */
    public function __call($name, $arguments) {
        // 匹配宽度
        if ( strpos( $name, 'setWidthCol' ) !==false ) {
            $style = str_replace( "setWidth", "", $name );
            if ( $arguments[0]<1 || $arguments[0]>12 ) $this->throwException( "容器宽度阈值错误" );
            $const = strtolower($style).$arguments[0];
            $width = WidgetConst::getConstant($const);
            // 匹配出media
            $pattern = "/^col([A-Za-z]*)[1-9]*/";
            preg_match($pattern,$const,$match);
            $media = $match[1];
            return $this->setWidth( $media, $width );
        }

        // 添加数据列表
        if ( $name == "addDataListTable" ) {
            $obj = new WidgetDataListTable();
            $sort = isset($arguments[0]) ? $arguments[0] : 0;
            $this->setChild( $obj, $sort );
            return $obj;
        }

        return $this;
    }

    /**
     * 设置宽度
     * @param $media
     * @param $width
     * @return $this
     */
    private function setWidth( $media, $width ) : WidgetTableContainer {
        $this->widthList[$media] = $width;
        return $this;
    }

    /**
     * 设置一个子容器
     * @param mixed $contentObj 子容器内容对象
     * @param int $sort
     *
     * @return WidgetTableContainer
     */
    private function setChild( &$contentObj, $sort=0 ) : WidgetTableContainer {
        if ( $sort ) {
            $this->childList[(int)$sort] = $contentObj;
        }else{
            $this->childList[] = $contentObj;
        }
        return $this;
    }

    /**
     * @return array
     */
    public function getWidthList(): array
    {
        return $this->widthList;
    }

}